
package br.com.model.daos;

import br.com.model.dbconector.DataBaseConnector;
import br.com.model.entidades.Usuario;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.LinkedList;
import java.util.List;


public class UsuarioDAO extends GenericDAO<Usuario> {

    @Override
    public void addEntity(Usuario entity) {
        
        String sql = "INSERT INTO Usuarios (nome,Username,Password) values (?,?,?)";
        //int toReturn = 0;
        if (entity != null) {
            try {
                PreparedStatement ps = DataBaseConnector.getInstance().getConnection().prepareStatement(sql);
                ps.setString(1, entity.getNome());
                ps.setString(2, entity.getUsername());
                ps.setString(3, entity.getPassword());
                ps.executeQuery();
                ps.close();
            } catch (SQLException sqle) {
                System.out.println("Erro ao Inserir Dados :" + sqle.getMessage());
            }
        }
        
        //return toReturn;
    }

    @Override
    public int updateEntity(Usuario entity) {
        String sql = "Update Usuarios set NOME=?, USERNAME=?, PASSWORD=? Where codigo=?";
        int toReturn = 0;
        if (entity != null) {
            try {
                PreparedStatement ps = DataBaseConnector.getInstance().getConnection().prepareStatement(sql);
                //ps.setInt(1, entity.getId());
                ps.setString(1, entity.getNome());
                ps.setString(2, entity.getUsername());
                ps.setString(3, entity.getPassword());
                ps.setInt(4, entity.getId());
                toReturn = ps.executeUpdate();
                ps.close();
            } catch (SQLException sqle) {
                System.out.println("Erro ao Atualizar Dados: " + sqle.getMessage());
            }
        }

        return toReturn;

    }

    @Override
    public Usuario getEntity(int primaryKey) {
        String sql = "Select * from USUARIOS where codigo = ?";
        Usuario toReturn = null;
        try {
            PreparedStatement ps = DataBaseConnector.getInstance().getConnection().prepareStatement(sql);
            ps.setInt(1, primaryKey);
            ResultSet rs = ps.executeQuery();
            toReturn = new Usuario();
            while (rs.next()) {
                toReturn.setId(rs.getInt(1));
                toReturn.setNome(rs.getString(2));
                toReturn.setUsername(rs.getString(3));
                toReturn.setPassword(rs.getString(4));
            }
            ps.close();
            rs.close();

        } catch (SQLException sqle) {
            System.out.println("Erro ao Buscar Dados: " + sqle.getMessage());
            toReturn = null;
        }
        return toReturn;
    }

    @Override
    public int removeEntity(int primaryKey) {
        String sql = "Delete from USUARIOS where codigo=?";
        int toReturn = 0;
        try {
            PreparedStatement ps = DataBaseConnector.getInstance().getConnection().prepareStatement(sql);
            ps.setInt(1, primaryKey);
            toReturn = ps.executeUpdate();
            ps.close();
        } catch (SQLException sqle) {
            System.out.println("Erro ao Remover Dados: " + sqle.getMessage());
            toReturn = 0;
        }
        return toReturn;
    }

    @Override
    public List<Usuario> getAllEntitys() {
        String sql = "Select * from Usuarios";
        List<Usuario> toReturn = new LinkedList<Usuario>();
        try {
            Statement st = DataBaseConnector.getInstance().getConnection().createStatement();
            ResultSet rs = st.executeQuery(sql);
            while (rs.next()) {
                Usuario usr = new Usuario();
                usr.setId(rs.getInt(1));
                usr.setNome(rs.getString(2));
                usr.setUsername(rs.getString(3));
                usr.setPassword(rs.getString(4));
                toReturn.add(usr);
            }
            st.close();
            rs.close();
        } catch (SQLException sqle) {
            System.out.println("Erro ao Carregar a Lista: " + sqle.getMessage());
        }
        return toReturn;
    }
    
    
}
